package com.beehoo.dcportal.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.RelatedLinkDao;
import com.beehoo.dcportal.admin.service.RelatedLinkService;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.RelatedLinkBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class RelatedLinkServiceImpl extends BaseService implements
		RelatedLinkService {
	private RelatedLinkDao relatedLinkDao;

	public void setRelatedLinkDao(RelatedLinkDao relatedLinkDao) {
		this.relatedLinkDao = relatedLinkDao;
	}

	@Override
	public void createRelatedLink(RelatedLinkBean relatedLinkBean)
			throws BaseException {
		validateRelatedLinkBean(relatedLinkBean);
		String id = relatedLinkDao.createRelatedLink(relatedLinkBean);
		if(CommonUtils.nvlToString(id).equals("")){
			throw new BaseException("custom.messages.error.link.create.error");
		}
	}

	@Override
	public void deleteRelatedLink(String linkId) throws BaseException {
		if(CommonUtils.nvlToString(linkId).equals("")){
			throw new BaseException("custom.messages.error.link.id.empty");
		}
		int ret = relatedLinkDao.deleteRelatedLink(linkId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.link.del.error");
		}
	}

	@Override
	public List<RelatedLinkBean> queryRelatedLink(String streetId,
			String linkType) throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("linkType", linkType);
		return relatedLinkDao.queryRelatedLink(paramMap);
	}

	private void validateRelatedLinkBean(RelatedLinkBean relatedLinkBean) throws BaseException{
		if(CommonUtils.nvlToString(relatedLinkBean.getLinkName()).equals("")){
			throw new BaseException("custom.messages.error.link.name.empty");
		}
		if(CommonUtils.nvlToString(relatedLinkBean.getLinkPath()).equals("")){
			throw new BaseException("custom.messages.error.link.path.empty");
		}
		if(CommonUtils.nvlToString(relatedLinkBean.getStreetId()).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
	}
}
